package com.bflow.web.controller;

import com.bflow.api.IBflowExtResService;
import com.bflow.api.IBflowRunTaskService;
import com.bflow.api.bo.BflowRunTaskBo;
import com.bflow.api.bo.BflowWaitDisposeBo;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.commons.bean.PageResult;
import com.bflow.util.JsonUtil;
import com.bflow.web.util.TestLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Name 首页
 * @Author haowen
 * @Date 2018-12-18
 */
@Controller
@RequestMapping(value = "/bflow/index")
public class BflowIndexController extends BflowBaseController {

    @Autowired
    private IBflowExtResService bflowExtResService;

    @Autowired
    private IBflowRunTaskService<?, BflowRunTaskBo> iBflowRunTaskService;

    @RequestMapping(value = "userList")
    @ResponseBody
    public String userList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageResult<BflowUserBo>  pageResult = bflowExtResService.findUserList(null);
            return JsonUtil.toStr(pageResult.getResult());
        } catch (Exception e) {
            return returnFail(e, "列表用户查询失败");
        }
    }

    @RequestMapping(value = "login")
    @ResponseBody
    public String login(String userId, HttpServletRequest req, HttpServletResponse resp) {
        TestLoginUtil.login(req, userId);
        return returnOk("登录成功");
    }

    @RequestMapping(value = "getUserInfo")
    @ResponseBody
    public String getUserInfo(HttpServletRequest req, HttpServletResponse resp) {
        BflowUserBo userBo = null;
        Long waitTaskCount = 0L;
        Map<String,Object> data = new HashMap<>();
        try {
            userBo = TestLoginUtil.getUser(req);
            //waitTaskCount = iBflowRunTaskService.getWaitTaskCount(userBo == null ? null : userBo.getUserId(), null);
            BflowWaitDisposeBo waitDisposeTaskList = iBflowRunTaskService.getWaitDisposeTaskList(userBo.getUserId(), null, null, userBo.getRoleKey());
            if(waitDisposeTaskList!=null){
                waitTaskCount=Long.valueOf(waitDisposeTaskList.getTasks().size());
            }
        } catch (Exception e) {
            return returnFail(e, "获取失败");
        }
        data.put("userBo",userBo);
        data.put("waitTaskCount",waitTaskCount);
        return returnOk(data, "获取成功");
    }

    @RequestMapping(value = "getWaitTaskCount")
    @ResponseBody
    public String getWaitTaskCount(HttpServletRequest req, HttpServletResponse resp) {
        Long waitTaskCount = 0L;
        try {
            BflowUserBo userBo = TestLoginUtil.getUser(req);
            waitTaskCount = iBflowRunTaskService.getWaitTaskCount(userBo == null ? null : userBo.getUserId(), null);
        } catch (Exception e) {
            return returnFail(e, "获取失败");
        }
        return returnOk(waitTaskCount, "获取成功");
    }

}
